package com.seio.edc.controller.energy;

import com.nova.edc.model.system.AreaPoint;
import com.nova.edc.model.system.CustomerArea;
import com.nova.edc.model.system.CustomerInfo;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.service.system.AreaPointService;
import com.nova.edc.service.system.CustomerAreaService;
import com.nova.edc.service.system.CustomerInfoService;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.DateUtils;
import com.nova.frame.utils.StringUtils;
import com.seio.edc.util.AssitDateUtil;
import com.seio.edc.util.DateUtil;
import com.seio.edc.util.StringUtil;
import org.apache.poi.hssf.record.formula.functions.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @param
 * @author <a href="mailto:huzhanjun@novacloud.com">huzj</a>
 * @version : 1.0
 * @date 2016-01-05 16:03
 */
@Controller
@RequestMapping(value = "/energy")
public class EnergyController {

    @Autowired
    private CustomerAreaService customerAreaService;
    @Autowired
    private AreaPointService areaPointService;
    @Autowired
    private CustomerInfoService novaCustomerInfoService;

    private static final int PAGE_SIZE = 10;

    @RequestMapping(value = "/AreaEnergy")
    public ModelAndView areaEnergy(ModelMap modelMap, Integer pageIndex, CustomerArea customerArea, String startDate, String endDate) {
        Integer customerinfoid = customerArea.getCustomerinfoid();
        CustomerInfo customerInfo = novaCustomerInfoService.getById(customerArea.getCustomerinfoid());
        modelMap.put("customerInfo", customerInfo);
        Page page = new Page();
        page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
        page.setPageSize(PAGE_SIZE);
        PageList<CustomerArea> customerAreaList = customerAreaService.findPageCustomerArea(customerArea, page, "ID", "asc");
        List<CustomerArea> customerAreas = customerAreaList.getDatas();
        // 时间初始化
        if (StringUtils.isEmpty(startDate)) {
            startDate = DateUtil.dateToString(AssitDateUtil.getAddDay(new Date(), -1));
        }
        if (StringUtils.isEmpty(endDate)) {
            endDate = DateUtil.dateToString(AssitDateUtil.getAddDay(new Date(), -1));
        }
        //customerAreaService.wrapEnergy(customerAreas, startDate, endDate);
        customerAreaService.wrapGather(customerAreas, startDate + " 00:00:00", endDate +  " 23:59:59");
        for (CustomerArea area : customerAreas) {
            List<Map<String, Object>> result = areaPointService.findDeviceDetail(area.getId());
            int lightCount = 0, airCount = 0;
            for (Map<String, Object> item : result) {
                String deviceName = (String) item.get("DeviceName");
                if (StringUtils.isNotNullOrEmpty(deviceName)) {
                    if (item.get("DeviceName").equals(ModelConstant.DEVICE_LIGHTENERGY)) {
                        lightCount = ((BigDecimal) item.get("Num")).intValue();
                    }
                    if (item.get("DeviceName").equals(ModelConstant.DEVICE_AIR)) {
                        airCount = ((BigDecimal) item.get("Num")).intValue();
                    }
                }

            }
            area.setLightCount(lightCount);
            area.setAirCount(airCount);
        }
        modelMap.put("startDate", startDate);
        modelMap.put("lighttype", ModelConstant.TRA_LIGHTTYPE);
        modelMap.put("airtype", ModelConstant.AIR_AIRTYPE);
        modelMap.put("endDate", endDate);
        modelMap.put("pageIndex", page.getCurrentPage());
        modelMap.put("dataCount", page.getRowCount());
        modelMap.put("pageCount", page.getTotalPage());
        modelMap.put("customerAreas", customerAreas);
        modelMap.put("customerArea", customerArea);
        return new ModelAndView("energy/AreaEnergy", modelMap);
    }
}
